package com.xiaoxu.common.exception;

import com.xiaoxu.common.result.Result;
import com.xiaoxu.common.result.ResultCodeEnum;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;



@ControllerAdvice //AOP 全局异常处理器
public class GlobalHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result err(Exception e){
        e.printStackTrace();
        return Result.fail().message("执行了全局处理异常");
    }
    //特定异常
    @ExceptionHandler(AccessDeniedException.class)
    @ResponseBody
    public Result error(AccessDeniedException e) throws AccessDeniedException {
        return Result.build(null, ResultCodeEnum.PERMISSION);
    }

    //自定义异常处理去
    @ExceptionHandler(XiaoXuException.class)
    @ResponseBody
    public Result err(XiaoXuException e){
        return Result.fail().code(e.getCode()).message("执行了自定义异常处理");
    }
}
